package com.peng.leetcode.slidingWindow;

/**
 * MinSubArrayLen
 * 209. 长度最小的子数组
 * https://leetcode.cn/problems/minimum-size-subarray-sum/
 * <p>
 * Created on 2022/12/11
 *
 * @author lupeng
 */
public class MinSubArrayLen {
    public int minSubArrayLen(int target, int[] nums) {
        int ans = Integer.MAX_VALUE;
        int sum = 0;
        int l = 0, r = 0;
        while (r < nums.length) {
            sum += nums[r];
            while (l <= r && sum >= target) {
                sum -= nums[l];
                ans = Math.min(ans, r - l + 1);
                l++;
            }
            r++;
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }
}
